﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using hsgsSHXX.DBUtility;//Please add references
using System.Collections.Generic;
namespace hsgsSHXX.DAL
{
	/// <summary>
	/// 数据访问类:gcpcs
	/// </summary>
	public partial class gcpcs
	{
		public gcpcs()
		{}
		#region  Method



		/// <summary>
		/// 增加一条数据
		/// </summary>
		public bool Add(hsgsSHXX.Model.gcpcs model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into gcpcs(");
			strSql.Append("jydw,jylx,lc,sbsj,mbsj,jxsj,cph,cxzw,sm)");
			strSql.Append(" values (");
			strSql.Append("@jydw,@jylx,@lc,@sbsj,@mbsj,@jxsj,@cph,@cxzw,@sm)");
			SqlParameter[] parameters = {
					new SqlParameter("@jydw", SqlDbType.NVarChar,255),
					new SqlParameter("@jylx", SqlDbType.NVarChar,255),
					new SqlParameter("@lc", SqlDbType.NVarChar,255),
					new SqlParameter("@sbsj", SqlDbType.DateTime),
					new SqlParameter("@mbsj", SqlDbType.DateTime),
					new SqlParameter("@jxsj", SqlDbType.DateTime),
					new SqlParameter("@cph", SqlDbType.NVarChar,255),
					new SqlParameter("@cxzw", SqlDbType.NVarChar,255),
					new SqlParameter("@sm", SqlDbType.NVarChar,255)};
			parameters[0].Value = model.jydw;
			parameters[1].Value = model.jylx;
			parameters[2].Value = model.lc;
			parameters[3].Value = model.sbsj;
			parameters[4].Value = model.mbsj;
			parameters[5].Value = model.jxsj;
			parameters[6].Value = model.cph;
			parameters[7].Value = model.cxzw;
			parameters[8].Value = model.sm;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(hsgsSHXX.Model.gcpcs model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update gcpcs set ");
			strSql.Append("jydw=@jydw,");
			strSql.Append("jylx=@jylx,");
			strSql.Append("lc=@lc,");
			strSql.Append("sbsj=@sbsj,");
			strSql.Append("mbsj=@mbsj,");
			strSql.Append("jxsj=@jxsj,");
			strSql.Append("cph=@cph,");
			strSql.Append("cxzw=@cxzw,");
			strSql.Append("sm=@sm");
			strSql.Append(" where ");
			SqlParameter[] parameters = {
					new SqlParameter("@jydw", SqlDbType.NVarChar,255),
					new SqlParameter("@jylx", SqlDbType.NVarChar,255),
					new SqlParameter("@lc", SqlDbType.NVarChar,255),
					new SqlParameter("@sbsj", SqlDbType.DateTime),
					new SqlParameter("@mbsj", SqlDbType.DateTime),
					new SqlParameter("@jxsj", SqlDbType.DateTime),
					new SqlParameter("@cph", SqlDbType.NVarChar,255),
					new SqlParameter("@cxzw", SqlDbType.NVarChar,255),
					new SqlParameter("@sm", SqlDbType.NVarChar,255)};
			parameters[0].Value = model.jydw;
			parameters[1].Value = model.jylx;
			parameters[2].Value = model.lc;
			parameters[3].Value = model.sbsj;
			parameters[4].Value = model.mbsj;
			parameters[5].Value = model.jxsj;
			parameters[6].Value = model.cph;
			parameters[7].Value = model.cxzw;
			parameters[8].Value = model.sm;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete()
		{
			//该表无主键信息，请自定义主键/条件字段
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from gcpcs ");
			strSql.Append(" where ");
			SqlParameter[] parameters = {
};

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public hsgsSHXX.Model.gcpcs GetModel()
		{
			//该表无主键信息，请自定义主键/条件字段
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  top 1 jydw,jylx,lc,sbsj,mbsj,jxsj,cph,cxzw,sm from gcpcs ");
			strSql.Append(" where ");
			SqlParameter[] parameters = {
};

			hsgsSHXX.Model.gcpcs model=new hsgsSHXX.Model.gcpcs();
			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				if(ds.Tables[0].Rows[0]["jydw"]!=null && ds.Tables[0].Rows[0]["jydw"].ToString()!="")
				{
					model.jydw=ds.Tables[0].Rows[0]["jydw"].ToString();
				}
				if(ds.Tables[0].Rows[0]["jylx"]!=null && ds.Tables[0].Rows[0]["jylx"].ToString()!="")
				{
					model.jylx=ds.Tables[0].Rows[0]["jylx"].ToString();
				}
				if(ds.Tables[0].Rows[0]["lc"]!=null && ds.Tables[0].Rows[0]["lc"].ToString()!="")
				{
					model.lc=ds.Tables[0].Rows[0]["lc"].ToString();
				}
				if(ds.Tables[0].Rows[0]["sbsj"]!=null && ds.Tables[0].Rows[0]["sbsj"].ToString()!="")
				{
					model.sbsj=DateTime.Parse(ds.Tables[0].Rows[0]["sbsj"].ToString());
				}
				if(ds.Tables[0].Rows[0]["mbsj"]!=null && ds.Tables[0].Rows[0]["mbsj"].ToString()!="")
				{
					model.mbsj=DateTime.Parse(ds.Tables[0].Rows[0]["mbsj"].ToString());
				}
				if(ds.Tables[0].Rows[0]["jxsj"]!=null && ds.Tables[0].Rows[0]["jxsj"].ToString()!="")
				{
					model.jxsj=DateTime.Parse(ds.Tables[0].Rows[0]["jxsj"].ToString());
				}
				if(ds.Tables[0].Rows[0]["cph"]!=null && ds.Tables[0].Rows[0]["cph"].ToString()!="")
				{
					model.cph=ds.Tables[0].Rows[0]["cph"].ToString();
				}
				if(ds.Tables[0].Rows[0]["cxzw"]!=null && ds.Tables[0].Rows[0]["cxzw"].ToString()!="")
				{
					model.cxzw=ds.Tables[0].Rows[0]["cxzw"].ToString();
				}
				if(ds.Tables[0].Rows[0]["sm"]!=null && ds.Tables[0].Rows[0]["sm"].ToString()!="")
				{
					model.sm=ds.Tables[0].Rows[0]["sm"].ToString();
				}
				return model;
			}
			else
			{
				return null;
			}
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select jydw,jylx,lc,sbsj,mbsj,jxsj,cph,cxzw,sm ");
			strSql.Append(" FROM gcpcs ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" jydw,jylx,lc,sbsj,mbsj,jxsj,cph,cxzw,sm ");
			strSql.Append(" FROM gcpcs ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM gcpcs ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DbHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T. desc");
			}
			strSql.Append(")AS Row, T.*  from gcpcs T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SqlParameter[] parameters = {
					new SqlParameter("@tblName", SqlDbType.VarChar, 255),
					new SqlParameter("@fldName", SqlDbType.VarChar, 255),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@IsReCount", SqlDbType.Bit),
					new SqlParameter("@OrderType", SqlDbType.Bit),
					new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
					};
			parameters[0].Value = "gcpcs";
			parameters[1].Value = "";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  Method
        public List<Model.gcpcs> GetFondListByPage(int pageSize, int pageIndex, string strWhere, string fOrderFiled, out int total)
        {
            if (fOrderFiled == "")
            {
                fOrderFiled = "jydw desc";
            }
            SqlParameter[] parameters = {  
                    new SqlParameter("@tableName", SqlDbType.VarChar, 50),  
                    new SqlParameter("@fields", SqlDbType.VarChar, 2000),  
                    new SqlParameter("@orderField", SqlDbType.VarChar, 500),  
                    new SqlParameter("@sqlWhere", SqlDbType.VarChar,2000),  
                    new SqlParameter("@pageSize", SqlDbType.Int),  
                    new SqlParameter("@pageIndex", SqlDbType.Int),  
                    new SqlParameter("@TotalPage", SqlDbType.Int),
                    new SqlParameter("@TotalRecordCount", SqlDbType.Int) 
                    };

            parameters[0].Value = "gcpcs";
            parameters[1].Value = "*";
            parameters[2].Value = fOrderFiled;
            parameters[3].Value = strWhere;
            parameters[4].Value = pageSize;
            parameters[5].Value = pageIndex;
            parameters[6].Direction = ParameterDirection.Output;
            parameters[7].Direction = ParameterDirection.Output;
            DataSet ds = DbHelperSQL.RunProcedure("up_GetRecordByPage", parameters, "ds", 60 * 30);
            total = int.Parse(parameters[7].Value.ToString());
            return DataTableToList(ds.Tables[0]);

        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<hsgsSHXX.Model.gcpcs> DataTableToList(DataTable dt)
        {
            List<hsgsSHXX.Model.gcpcs> modelList = new List<hsgsSHXX.Model.gcpcs>();
            int rowsCount = dt.Rows.Count;
            if (rowsCount > 0)
            {
                hsgsSHXX.Model.gcpcs model;
                for (int n = 0; n < rowsCount; n++)
                {
                    model = new hsgsSHXX.Model.gcpcs();
                    if (dt.Rows[n]["jydw"] != null && dt.Rows[n]["jydw"].ToString() != "")
                    {
                        model.jydw = dt.Rows[n]["jydw"].ToString();
                    }
                    if (dt.Rows[n]["jylx"] != null && dt.Rows[n]["jylx"].ToString() != "")
                    {
                        model.jylx = dt.Rows[n]["jylx"].ToString();
                    }
                    if (dt.Rows[n]["lc"] != null && dt.Rows[n]["lc"].ToString() != "")
                    {
                        model.lc = dt.Rows[n]["lc"].ToString();
                    }
                    if (dt.Rows[n]["sbsj"] != null && dt.Rows[n]["sbsj"].ToString() != "")
                    {
                        model.sbsj = DateTime.Parse(dt.Rows[n]["sbsj"].ToString());
                    }
                    if (dt.Rows[n]["mbsj"] != null && dt.Rows[n]["mbsj"].ToString() != "")
                    {
                        model.mbsj = DateTime.Parse(dt.Rows[n]["mbsj"].ToString());
                    }
                    if (dt.Rows[n]["jxsj"] != null && dt.Rows[n]["jxsj"].ToString() != "")
                    {
                        model.jxsj = DateTime.Parse(dt.Rows[n]["jxsj"].ToString());
                    }
                    if (dt.Rows[n]["cph"] != null && dt.Rows[n]["cph"].ToString() != "")
                    {
                        model.cph = dt.Rows[n]["cph"].ToString();
                    }
                    if (dt.Rows[n]["cxzw"] != null && dt.Rows[n]["cxzw"].ToString() != "")
                    {
                        model.cxzw = dt.Rows[n]["cxzw"].ToString();
                    }
                    if (dt.Rows[n]["sm"] != null && dt.Rows[n]["sm"].ToString() != "")
                    {
                        model.sm = dt.Rows[n]["sm"].ToString();
                    }
                    modelList.Add(model);
                }
            }
            return modelList;
        }
	}
}

